import {
  wxRequest
} from '../../utils/server.js';
const QR = require('../../utils/wxqrcode.js');
const _wxRequest = new wxRequest();
const app = getApp();
let menuList = [{
    name: '我的笔记',
    url: '/pages/square/list',
    auth: 'vlog:mgt'
  }, {
    name: '笔记管理',
    url: '/pages/square/audit/list',
    auth: 'vlog:audit'
  }, {
    name: '笔记分类管理',
    url: '/pages/square/category/list',
    auth: 'vlog:category:mgt'
  }, {
    name: '我的分润',
    url: '/pages/menber/settle/my',
    auth: 'mine:settle'
  }, {
    name: '商品',
    url: '/pages/publish/goods/list',
    auth: 'goods:mgt'
  }, {
    name: '商品分类',
    url: '/pages/publish/goodscat/list',
    auth: 'gdsCat:mgt'
  }, {
    name: '品牌管理',
    url: '/pages/publish/brand/list',
    auth: 'brand:mgt'
  }, {
    name: '商品组',
    url: '/pages/publish/goodsgroup/list',
    auth: 'gdsGroup:mgt'
  }, {
    name: '连锁总部',
    url: '/pages/publish/chain_store/list',
    auth: 'chain_store:mgt'
  }, {
    name: '体验店',
    url: '/pages/publish/store/list',
    auth: 'store:mgt'
  }, {
    name: '区域经理',
    url: '/pages/publish/region/list',
    auth: 'region:mgt'
  }, {
    name: '代理商',
    url: '/pages/publish/agent/list',
    auth: 'agent:mgt'
  }, {
    name: '供应商',
    url: '/pages/publish/supply/list',
    auth: 'sup:mgt'
  }, {
    name: app.globalData.memeberText,
    url: '/pages/publish/staff/list',
    auth: 'emp:mgt'
  }, {
    name: '结算分润',
    url: '/pages/menber/distribut/close',
    auth: 'settle:do'
  }, {
    name: '分销状况',
    url: '/pages/menber/distribut/audit',
    auth: 'store:share:audit'
  }, {
    name: '广告列表',
    url: '/pages/publish/adpicture/list',
    auth: 'ad:mgt'
  }, {
    name: '活动资讯列表',
    url: '/pages/publish/cmsinfo/list',
    auth: 'info:mgt'
  }, {
    name: '首单礼发放',
    url: '/pages/presentOdr/index',
    auth: 'present:list'
  }, {
    name: '数据',
    url: '/pages/data/index',
    auth: 'data:view'
  },
  {
    name: '设备类型',
    url: '/pages/publish/device/type',
    auth: 'deviceType:mgt'
  },
  {
    name: '绑定设备列表',
    url: '/pages/publish/bind_device/list',
    auth: 'device:bind'
    //auth:'device:bind'
  },
  {
    name: '绑定设备记录',
    url: '/pages/publish/bind_device/record',
    auth: 'device:bind_record'
    //auth:'device:bind'
  },
  {
    name: '色板列表',
    url: '/pages/publish/colour_atla/list',
    auth: 'dyeColor:mgt'
  },
  {
    name: '资金账户',
    url: '/pages/finance_account/finance_index',
    auth: 'fin:account:view:mine'
  },
  {
    name: '资金账户开通审核',
    url: '/pages/finance_account/account_audit/account_audit',
    auth: 'fin:account:audit:list'
  },
  {
    name: '资金账户提现审核',
    url: '/pages/finance_account/withdraw_audit/withdraw_audit',
    auth: 'withdraw:list:view'
  },
  // {
  //   name: '关于设备说明',
  //   url: '/pages/about_device/list',
  //   auth: 'device:about:view'
  // },
];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    userInfo: wx.getStorageSync('userInfo') || {},
    uploadUrl: wx.getStorageSync('uploadUrl'),
    toplist: [{
        name: '采购',
        url: '/pages/menber/purchase/index',
        icon: '/icon/daifukuan.png',
        auth: "pur:odr:create"
      },
      // {
      //   name: '采购有礼',
      //   url: '/pages/order/index?present=1',
      //   icon: '/icon/liwu.png',
      //   auth: "present:set"
      // },
      {
        name: '订单',
        url: '/pages/shop_order/index?present=1',
        icon: '/icon/liwu.png',
        auth: "mine:order"
      },
      {
        name: '售后',
        url: '/pages/menber/afterSale/index',
        icon: '/icon/pingjia.png',
        auth: "odr:service"
      },
      {
        name: '发货',
        url: '/pages/menber/mall/order',
        icon: '/icon/daishouhuo.png',
        auth: "odr:ship"
      },
      {
        name: '分销',
        url: '/pages/menber/distribut/index',
        icon: '/icon/shouhou.png',
        auth: "store:share:req"
      },
      {
        name: '卡包',
        url: '/pages/coupon_package/coupon_index',
        icon: '/icon/daifukuan.png',
        auth: "card:package:view"
        // auth: "card:package"
      },
    ],
    list: [],
    qrcode: '',
    qrcodeShow: false
  },

  onLoad: function () {
    if (wx.getStorageSync('devId')) {
      wx.removeStorageSync('devId')
    }
  },
  onShow: function () {
    this.getUserInfo();
  },

  openQrCode: function (e) {
    var that = this;
    let qrcodeSize = that.getQRCodeSize()
    
    let value = that.data.userInfo.orgId
    that.createQRCode(value, qrcodeSize)
    if (this.data.qrcode !== '') {
      this.setData({
        qrcodeShow: true
      })
    }
  },

  /**
   * 点击取消
   */
  closeQrcode: function () {
    this.setData({
      qrcodeShow: false
    })
  },

  //适配不同屏幕大小的canvas
  getQRCodeSize: function () {
    var size = 0;
    try {
      var res = wx.getSystemInfoSync();
      var scale = res.windowWidth / 750; //不同屏幕下QRcode的适配比例；设计稿是750宽
      var width = 300 * scale;
      size = width;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
      size = 150;
    }
    return size;
  },
  createQRCode: function (text, size) {
    //调用插件中的draw方法，绘制二维码图片
    let that = this
    try {
      // console.log('QRcode: ', text, size)
      let _img = QR.createQrCodeImg(text, {
        size: parseInt(size)
      })
      that.setData({
        'qrcode': _img
      })
    } catch (e) {
      console.log(e)
    }
  },

  //页面跳转
  toPage: function (e) {
    var url = e.currentTarget.dataset.url;
    console.log(url);
    wx.navigateTo({
      url,
    })
  },


  //跳转到订单详情
  toOrder: function (e) {
    var status = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/order/index?status=' + status,
    })
  },

  //去设置
  toSetting: function () {
    wx.navigateTo({
      url: '/pages/menber/setting/index',
    })
  },

  //查询列表
  getlist: function () {

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/device/findType',
      method: 'POST',
      data: {}
    }, (res) => {
      let currentMenuList = JSON.parse(JSON.stringify(menuList));
      let list = res.list;
      list.map(item => {
        currentMenuList.push({
          name: item.typeName,
          url: '/pages/sub_menu/index?typeId=' + item.id + '&correct=' + item.correct + '&note=' + item.note,
        })
      })
      this.setData({
        list: currentMenuList
      })
    })
  },

  //获取用户信息
  getUserInfo: function () {
    wx.removeStorageSync('userInfo');
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/user/getLoginUser',
      method: 'get',
    }, (res) => {
      wx.hideLoading()
      this.setData({
        userInfo: res.vo
      })
      this.getlist();


      wx.setStorageSync('userInfo', res.vo);
      this.getAuthorizedItem();
      console.log(res, '用户信息')
    })
  },

  //获取当前登录用户的所有权限
  getAuthorizedItem: function () {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/structure/getAuthorizedItem',
      method: 'get',
    }, (res) => {
      wx.hideLoading()
      console.log(res)
      var list = res.list;
      var authorized = {}
      list.map(item => {
        var obj = {
          [item.authCode]: item.name
        }
        authorized = Object.assign(authorized, obj)
      })
      that.$globalSetData({
        permissions: authorized
      })
      wx.setStorageSync('authorized', authorized);

    })
  },




  //商品关注
  arrGds: function () {
    wx.navigateTo({
      url: '/pages/attention/goods',
    })
  },

  arrStore: function () {
    wx.navigateTo({
      url: '/pages/attention/store',
    })
  },











})